<template>
    <div id="OrdersTitle">
        <v-details :inputConfig="inputConfig" :detailData="inputData" :rules="rules" @pop="setdata"
                   ref="input"></v-details>
    </div>
</template>

<script>
import details from 'commons/Detail'
import baseMixin from "@/mixins/BaseMixin";

export default {
    props: {
        inputData: {
            type: Object,
        }
    },
    mixins: [baseMixin],
    name: "OrdersTitle",
    data() {
        return {
            addflag: true,
            rules: {
                type: [
                    {required: true, trigger: 'blur'},
                ],
                fromstorerkey: [
                    {required: true, validator: this.storerkey, trigger: 'blur'},
                ],
                tostorerkey: [
                    {required: true, validator: this.storerkey, trigger: 'blur'},
                ],
            },
        }
    },
    computed: {
        inputConfig() {
            return [
                [
                    {
                        type: "input",
                        label: this.$t('ex.transferkey'),
                        props: "transferkey",
                        disabled: true
                    },
                    {
                        type: "input",
                        label: this.$t('in.externreceiptkey2'),
                        props: "referencenumber",
                        disabled: this.addflag
                    },
                    {
                        type: "table",
                        label: this.$t('ex.fromstorerkey'),
                        props: "fromstorerkey",
                        tableConfig: [
                            {
                                label: this.$t('config.shipper'),
                                prop: "storerkey",
                                input: "input",
                            },
                            {
                                label: this.$t('config.company'),
                                prop: "company",
                                input: "input",
                            },
                            {
                                label: this.$t('config.city'),
                                prop: "city",
                                input: "input",
                            },
                            {
                                label: this.$t('config.provice'),
                                prop: "state",
                                input: "input",
                            },
                            {
                                label: this.$t('config.postcode'),
                                prop: "zip",
                                input: "input",
                            },
                            {
                                label: this.$t('config.contact'),
                                prop: "contact",
                                input: "input",
                            },
                            {
                                label: this.$t('config.phone'),
                                prop: "phone",
                                input: "input",
                            }
                        ],
                        url: `/queryStorer/1/?/${this.$store.state.wms}`,
                        disabled: this.addflag
                    },
                ],
                [
                    {
                        type: "select",
                        label: this.$t('common.type'),
                        props: "type",
                        options: this.getOptions("codelkup/TRANTYPE")
                    },
                    {
                        type: "input",
                        label: this.$t('ex.referencenumber',{no:2}),
                        props: "referencenumber2",
                        disabled: this.addflag
                    },
                    {
                        type: "table",
                        label: this.$t('ex.tostorerkey'),
                        props: "tostorerkey",
                        tableConfig: [
                            {
                                label: this.$t('config.shipper'),
                                prop: "storerkey",
                                input: "input",
                            },
                            {
                                label: this.$t('config.company'),
                                prop: "company",
                                input: "input",
                            },
                            {
                                label: this.$t('config.city'),
                                prop: "city",
                                input: "input",
                            },
                            {
                                label: this.$t('config.provice'),
                                prop: "state",
                                input: "input",
                            },
                            {
                                label: this.$t('config.postcode'),
                                prop: "zip",
                                input: "input",
                            },
                            {
                                label: this.$t('config.contact'),
                                prop: "contact",
                                input: "input",
                            },
                            {
                                label: this.$t('config.phone'),
                                prop: "phone",
                                input: "input",
                            }
                        ],
                        url: `/queryStorer/1/?/${this.$store.state.wms}`,
                        disabled: this.addflag
                    },
                ],
                [
                    {
                        type: "select",
                        label: this.$t('common.status'),
                        props: "status",
                        options: this.getOptions("codelkup/TRANSTATUS"),
                        disabled: true
                    },
                    {
                        type: "textarea",
                        label:  this.$t('app.remark'),
                        props: "notes",
                    }
                ],
            ]

        }
    },
    methods: {
        storerkey(rule, value, callback) {
            if (value === '') {
                callback(new Error());
            } else {
                this.$api.get(`/isExists/1/${value}/${this.$store.state.wms}`).then(res => {
                    
                        if (parseInt(res.bean) <= 0) {
                            this.$message.error(this.$t('config.nullShipper') + `:${value}`);
                            this.inputData[rule.field] = "";
                            callback(new Error());
                        } else {
                            callback()
                        }
                }).catch(e => {
                    callback(new Error());
                })
            }
        },
        setdata(data, id, close, listId) {
            if (listId == 0) {
                this.inputData.fromstorerkey = data.storerkey
            } else if (listId == 1) {
                this.inputData.tostorerkey = data.storerkey
            }
            close(false);
        }
    },
    mounted() {
        if (Object.keys(this.$store.state.skip).length !== 0) {
            let data = this.$store.state.skip;
            if (!data.newFlag) {
                this.addflag = true;
            } else {
                this.addflag = false;
            }
        } else if (!this.$store.state.newFlag) {
            this.addflag = true;
        } else {
            this.addflag = false;
        }
    },
    watch: {},
    components: {
        'v-details': details
    }
}
</script>

<style scoped>

</style>
